﻿Public Class danhsachmathang

    Private Property DSSanPhamBS As DSSanPhamBS
    Private DSSanPhamGD As DSSanPhamGD
    Private cm As CurrencyManager


    '--------- PageLoad

    Private Sub danhsachmathang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadDGV_DSSanPhamAD()
        LoadComboBoxNhomMatHang()
        LoadComboBoxHangSX()
        dgrvDSMatHangAD.Columns(0).HeaderText = "Mã SP"
        dgrvDSMatHangAD.Columns(1).HeaderText = "Tên SP"
        dgrvDSMatHangAD.Columns(2).HeaderText = "Tên Nhóm SP"
        dgrvDSMatHangAD.Columns(3).HeaderText = "Tên Hãng SX"
        dgrvDSMatHangAD.Columns(4).HeaderText = "Đơn Vị Tính"
        dgrvDSMatHangAD.Columns(5).HeaderText = "Số Lượng"
        dgrvDSMatHangAD.Columns(6).HeaderText = "Đơn Giá"

    End Sub

    '--------- Load

    Private Sub LoadDGV_DSSanPhamAD()
        Dim DSSanPhamList As New List(Of DSSanPhamGD)
        DSSanPhamBS = New DSSanPhamBS
        DSSanPhamList = DSSanPhamBS.GetData()
        dgrvDSMatHangAD.DataSource = DSSanPhamList
        dgrvDSMatHangAD.ClearSelection()
    End Sub

    Private Sub LoadComboBoxNhomMatHang()
        Dim i As Integer
        Dim connect As New DataControl
        Dim dt As New DataTable
        Dim maNhomSanPham As String
        Dim NhomSanPhamGD As New NhomSanPhamGD
        Dim List As New List(Of NhomSanPhamGD)
        dt = connect.GetData("Select MaNhomSanPham, TenNhomSanPham FROM NHOMSANPHAM")
        For i = 0 To dt.Rows.Count() - 1
            maNhomSanPham = dt.Rows(i)("TenNhomSanPham").ToString
            NhomSanPhamGD = New NhomSanPhamGD(maNhomSanPham)
            List.Add(NhomSanPhamGD)

        Next
        cbbnhommathang.DataSource = List
        cbbnhommathang.DisplayMember = "MaNhomSanPham"
    End Sub

    Private Sub LoadComboBoxHangSX()
        Dim i As Integer
        Dim connect As New DataControl
        Dim dt As New DataTable
        Dim maHangSX As String
        Dim HangSanXuatGD As New HangSanXuatGD
        Dim List As New List(Of HangSanXuatGD)
        dt = connect.GetData("Select MaHangSX, TenHangSX FROM HANGSANXUAT")
        For i = 0 To dt.Rows.Count() - 1
            maHangSX = dt.Rows(i)("TenHangSX").ToString
            HangSanXuatGD = New HangSanXuatGD(maHangSX)
            List.Add(HangSanXuatGD)
        Next
        cbbhangsx.DataSource = List
        cbbhangsx.DisplayMember = "MaHangSX"
    End Sub

    Private Sub ClearTextBox()
        txtmamathang.Text = ""
        txttenmathang.Text = ""
        cbbnhommathang.Text = ""
        cbbhangsx.Text = ""
        txtdongia.Text = ""
        txtsoluong.Text = ""
        txtdonvitinh.Text = ""
    End Sub

    Private Sub ClearBinding()
        txtmamathang.DataBindings.Clear()
        txttenmathang.DataBindings.Clear()
        cbbnhommathang.DataBindings.Clear()
        cbbhangsx.DataBindings.Clear()
        txtdongia.DataBindings.Clear()
        txtsoluong.DataBindings.Clear()
        txtdonvitinh.DataBindings.Clear()
    End Sub



    '--------- Cac Button

    Private Sub btnthemmathang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthemmathang.Click
        Me.GroupBox1.Visible = False
        Me.GroupBox2.Visible = True
        btncapnhatmathang.Visible = False
        ClearTextBox()
        ClearBinding()
        txtsoluong.Text = "0"
        txtsoluong.Enabled = False
    End Sub

    Private Sub btnsuamathang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsuamathang.Click
        Me.GroupBox1.Visible = False
        Me.GroupBox2.Visible = True
        txtmamathang.Enabled = False
        btnaddmathang.Visible = True

        Dim maSanPham As String
        maSanPham = dgrvDSMatHangAD.SelectedRows.Item(0).Cells(0).Value
        Dim tenSanPham As String
        tenSanPham = dgrvDSMatHangAD.SelectedRows.Item(0).Cells(1).Value
        Dim maNhomSanPham As String
        maNhomSanPham = dgrvDSMatHangAD.SelectedRows.Item(0).Cells(2).Value
        Dim maHangSX As String
        maHangSX = dgrvDSMatHangAD.SelectedRows.Item(0).Cells(3).Value
        Dim donViTinh As String
        donViTinh = dgrvDSMatHangAD.SelectedRows.Item(0).Cells(4).Value
        Dim soLuong As Integer
        soLuong = dgrvDSMatHangAD.SelectedRows.Item(0).Cells(5).Value
        Dim donGia As Double
        donGia = dgrvDSMatHangAD.SelectedRows.Item(0).Cells(6).Value

        txtmamathang.Text = maSanPham
        txttenmathang.Text = tenSanPham
        cbbnhommathang.Text = maNhomSanPham
        cbbhangsx.Text = maHangSX
        txtdonvitinh.Text = donViTinh
        txtsoluong.Text = soLuong
        txtdongia.Text = donGia
        txtsoluong.Enabled = False

    End Sub

    Private Sub btnxoamathang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnxoamathang.Click
        Dim maSanPham As String
        maSanPham = dgrvDSMatHangAD.SelectedRows.Item(0).Cells(0).Value
        Dim tenSanPham As String
        tenSanPham = dgrvDSMatHangAD.SelectedRows.Item(0).Cells(1).Value
        Dim maNhomSanPham As String
        maNhomSanPham = dgrvDSMatHangAD.SelectedRows.Item(0).Cells(2).Value
        Dim maHangSX As String
        maHangSX = dgrvDSMatHangAD.SelectedRows.Item(0).Cells(3).Value
        Dim donViTinh As String
        donViTinh = dgrvDSMatHangAD.SelectedRows.Item(0).Cells(4).Value
        Dim soLuong As Integer
        soLuong = dgrvDSMatHangAD.SelectedRows.Item(0).Cells(5).Value
        Dim donGia As Double
        donGia = dgrvDSMatHangAD.SelectedRows.Item(0).Cells(6).Value
        DSSanPhamGD = New DSSanPhamGD(maSanPham, tenSanPham, maNhomSanPham, maHangSX, donViTinh, soLuong, donGia)
        DSSanPhamBS.Delete(DSSanPhamGD)
        Me.GroupBox1.Visible = True
        Me.GroupBox2.Visible = False
        LoadDGV_DSSanPhamAD()
    End Sub

    Private Sub btnthoatmathang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthoatmathang.Click
        Me.Close()
    End Sub

    Private Sub btnaddmathang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnaddmathang.Click
        Dim maSanPham As String = txtmamathang.Text
        Dim tenSanPham As String = txttenmathang.Text
        Dim maNhomSanPham As String = cbbnhommathang.Text
        Dim maHangSX As String = cbbhangsx.Text
        Dim donViTinh As String = txtdonvitinh.Text
        Dim soLuong As Integer = txtsoluong.Text
        Dim donGia As Double = txtdongia.Text

        'Test
        'Dim dt As New DataTable
        'Dim connect As New DataControl
        'dt = connect.GetData("Select MaNhomSanPham FROM NHOMSANPHAM WHERE TenNhomSanPham LIKE '%" + cbbnhommathang.Text + "%'")
        'For i = 0 To dt.Rows.Count() - 1
        'homSanPham = dt.Rows(i)("MaNhomSanPham").ToString
        ' Next

        'dt = connect.GetData("Select MaHangSX FROM HANGSANXUAT WHERE TenHangSX LIKE '%" + cbbhangsx.Text + "%'")
        'For i = 0 To dt.Rows.Count() - 1
        'aHangSX = dt.Rows(i)("MaHangSX").ToString
        'Next


        DSSanPhamGD = New DSSanPhamGD(maSanPham, tenSanPham, maNhomSanPham, maHangSX, donViTinh, soLuong, donGia)
        DSSanPhamBS.Insert(DSSanPhamGD)
        Me.GroupBox1.Visible = True
        Me.GroupBox2.Visible = False
        LoadDGV_DSSanPhamAD()
        btncapnhatmathang.Visible = True
        btnaddmathang.Visible = True


    End Sub

    Private Sub btnbackmathang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbackmathang.Click
        ClearBinding()
        LoadDGV_DSSanPhamAD()
        Me.GroupBox1.Visible = True
        Me.GroupBox2.Visible = False
        btncapnhatmathang.Visible = True
        btnaddmathang.Visible = True
    End Sub

    Private Sub btntimmathang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntimmathang.Click
        dgrvDSMatHangAD.DataSource = DSSanPhamBS.GetByName(txttimmathang.Text)
    End Sub


    '---------- Các Sự Kiện

    Private Sub txttimmathang_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles txttimmathang.MouseClick
        radiotimmathang.Checked = True
    End Sub

    Private Sub radiotimmathang_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles radiotimmathang.MouseClick
        txttimmathang.Focus()
    End Sub

    Private Sub RadioButton1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RadioButton1.MouseClick
        LoadDGV_DSSanPhamAD()
    End Sub
End Class